import { defineConfig, loadEnv } from 'vite';
import createVitePlugins from './plugins/index';
import { wrapperEnv } from './plugins/utils';
import { createProxy } from './plugins/proxy';
import createBuildChunks from './plugins/build-chunks';
import postCssPxToRem from 'postcss-pxtorem';
import autoprefixer from 'autoprefixer';
import { resolve } from 'path';
import dayjs from 'dayjs';

// https://vitejs.dev/config/
// export default defineConfig({
//   plugins: [react()],
// })
const __APP_INFO__ = {
    lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
};

export default defineConfig(({ command, mode }: any): any => {
    const root = process.cwd();
    const isBuild = command === 'build';
    const env = loadEnv(mode, root);
    const viteEnv = wrapperEnv(env);
    const { VITE_BASE_URL, VITE_PORT, VITE_PROXY, VITE_BASE_HOST } = viteEnv;
    const { manualChunks } = createBuildChunks(viteEnv, isBuild);

    return {
        root,
        plugins: createVitePlugins(viteEnv, isBuild),
        // 开启本地服务
        server: {
            base: 'https://chenhaojie.com',
            // 本地服务器的域名
            host: 'chenhaojie-dashauibi.com',
            // 本地端口
            port: '3001',
            // 自动打开
            open: true,
            // 映射 代理
            proxy: createProxy(VITE_PROXY),
        },
        define: {
            __APP_INFO__: JSON.stringify(__APP_INFO__),
            'process.env': process.env,
        },
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src'),
                '@component': resolve(__dirname, 'src/component'),
                '@router': resolve(__dirname, 'src/router'),
                '@view': resolve(__dirname, 'src/view'),
            },
        },
        css: {
            modules: {
                localsConvention: 'dashesOnly',
            },
            preprocessorOptions: {
                less: {
                    math: 'always',
                    globalVars: {
                        blue: '#1CC0FF',
                    },
                    javascriptEnabled: true,
                },
            },
            postcss: {
                // autoprefixer(),
                plugins: [
                    // 自动添加css浏览器前缀
                    autoprefixer(),
                    // postCssPxToRem({
                    //     rootValue: 20,
                    // 	unitPrecision: 5,
                    // 	propList: ['*','!border*'],
                    // 	selectorBlackList: ['van-'],
                    // 	replace: true,
                    // 	mediaQuery: false,
                    // 	minPixelValue: 1.1,
                    // }),
                    {
                        postcssPlugin: 'internal:charset-removal',
                        AtRule: {
                            charset: (atRule) => {
                                if (atRule.name === 'charset') {
                                    atRule.remove();
                                }
                            },
                        },
                    },
                ],
            },
        },
        build: {
            emptyOutDir: true,
            // 资源
            assetsDir: 'static/assets/',
            brotliSize: false,
            minify: 'terser',
            chunkSizeWarningLimit: 500,
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    // drop_console: true,
                },
            },
            rollupOptions: {
                output: {
                    entryFileNames: 'static/js/[name]-[hash].js',
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    manualChunks,
                },
            },
        },
    };
});
